package top.italks.esp32app.common;

import static top.italks.esp32app.common.Constant.MESSAGE_SERIAL;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.blankj.utilcode.util.ConvertUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class BluetoothSocketThread extends Thread{
    private String TAG="BluetoothSocketThread";
    private String deviceMac;
    private boolean isRunning=true;
    private BluetoothSocket bluetoothSocket;
    private InputStream inputStream;
    // 用来发数据
    private OutputStream outputStream;

    private Thread receThread;
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothDevice bluetoothDevice;
    private Handler handler;
    public BluetoothSocketThread(String deviceMac, Handler handler) {
        this.deviceMac = deviceMac;
        this.handler = handler;
        bluetoothAdapter=BluetoothAdapter.getDefaultAdapter();
        bluetoothDevice = bluetoothAdapter.getRemoteDevice(deviceMac);
        connect(bluetoothDevice);
    }

    @Override
    public void run() {
        super.run();
        // 开始监听数据接收
        try {
            InputStream inputStream = bluetoothSocket.getInputStream();
            byte[] result = new byte[0];
            while (isRunning) {
                byte[] buffer = new byte[256];
                // 等待有数据
                while (inputStream.available() == 0 && isRunning) {if (System.currentTimeMillis() < 0) break;}
                while (isRunning) {
                    try {
                        int num = inputStream.read(buffer);
                        byte[] temp = new byte[num];
                        System.arraycopy(buffer, 0, temp, 0, num);
                        result = temp;
                        if (inputStream.available() == 0) break;
                    } catch (Exception e) {
                        e.printStackTrace();
                        // todo:处理接收数据单次失败
                        break;
                    }
                }
                try {
                    // 返回数据
//                    Log.d(TAG,"当前累计收到的数据=>" + ConvertUtils.bytes2String(result));
                    Message message=Message.obtain();
                    message.what=MESSAGE_SERIAL;
                    message.obj=ConvertUtils.bytes2String(result);
                    handler.dispatchMessage(message);
                } catch (Exception e) {
                    e.printStackTrace();
                    // todo:处理验证收到数据结束标志出错
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            // todo:处理接收数据失败
        }
    }
    private void connect(BluetoothDevice device){
        try {
            bluetoothSocket = device.createRfcommSocketToServiceRecord(Constant.SPP_UUID);
            bluetoothSocket.connect();
            // 用来收数据
            inputStream = bluetoothSocket.getInputStream();
            // 用来发数据
            outputStream = bluetoothSocket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
